package com.inventive.system.monitoring.gwt.server.service.statistics;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.MultiTask;
import com.inventive.system.monitoring.gwt.client.statistics.commands.AddPublisherAction;
import com.inventive.system.monitoring.gwt.client.statistics.commands.AddPublisherResult;
import com.inventive.system.monitoring.gwt.server.service.action.ActionHandler;
import com.inventive.system.monitoring.server.service.publisher.callable.AddPublisherCallable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.ExecutorService;

/**
 * User: grant
 */
@Service
public class AddPublisherActionHandler implements ActionHandler<AddPublisherAction, AddPublisherResult> {

    @Autowired
    @Qualifier("system-monitoring")
    private HazelcastInstance hazelcastInstance;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public AddPublisherResult execute(AddPublisherAction action) {

        ExecutorService executorService = hazelcastInstance.getExecutorService("publishers");

        executorService.execute(new MultiTask<Boolean>(new AddPublisherCallable(action.getJmxStatisticKey()), hazelcastInstance.getCluster().getMembers()));

        return new AddPublisherResult();
    }

    @Override
    public Class<AddPublisherAction> getActionType() {
        return AddPublisherAction.class;
    }
}
